<?php
namespace Admin\Controller;

use Think\Controller;
use Admin\Model\AuthRuleModel;
use Admin\Model\AuthGroupModel;
use Think\Model;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/7/25
 * Time: 9:31
 */
class SpShopforoutlinkController extends Controller
{
    public function  shopsellsummary()
    {
        //获取当前年月
        $thisMonth = date('Ym');
        $thisDayBegin = strtotime(date('Y-m-d') . '00:00');
        $thisDayEnd = strtotime(date('Y-m-d') . '23:59');
        $yeaterdayBegin = strtotime(date("Y-m-d", strtotime("-1 day")) . '00:00');
        $yeaterdayEnd = strtotime(date("Y-m-d", strtotime("-1 day")) . '23:59');
        $thisMonthBegin = strtotime(date('Y-m') . '-01 00:00');
        $thisMonthEnd = strtotime(date('Y-m-d H:i:s'));

        $sqlThisDaySell = "select o.shopId,s.name,SUM(o.finnalPrice) as totalSell from gr_sp_order o inner join gr_sp_shops s on o.shopId=s.id WHERE  o.createat>$thisDayBegin and o.createat<$thisDayEnd and (o.type=1 or o.type=5) and o.status=1 group by o.shopId";
        $sqlThisDaySell_Tui = "select o.shopId,s.name,SUM(o.finnalPrice) as totalSell from gr_sp_order o inner join gr_sp_shops s on o.shopId=s.id WHERE  o.createat>$thisDayBegin and o.createat<$thisDayEnd and o.type=3 and o.status=1 group by o.shopId";
        $sqlYeaterdaySell = "select o.shopId,s.name,SUM(o.finnalPrice) as totalSell from gr_sp_order o inner join gr_sp_shops s on o.shopId=s.id WHERE  o.createat>$yeaterdayBegin and o.createat<$yeaterdayEnd and (o.type=1 or o.type=5) and o.status=1 group by o.shopId";
        $sqlYeaterdaySell_Tui = "select o.shopId,s.name,SUM(o.finnalPrice) as totalSell from gr_sp_order o inner join gr_sp_shops s on o.shopId=s.id WHERE  o.createat>$yeaterdayBegin and o.createat<$yeaterdayEnd and o.type=3 and o.status=1 group by o.shopId";
        $sqlThisMonthSell = "select o.shopId,s.name,SUM(o.finnalPrice) as totalSell from gr_sp_order o inner join gr_sp_shops s on o.shopId=s.id WHERE  o.createat>$thisMonthBegin and o.createat<$thisMonthEnd and (o.type=1 or o.type=5) and o.status=1 group by o.shopId";
        $sqlThisMonthSell_Tui = "select o.shopId,s.name,SUM(o.finnalPrice) as totalSell from gr_sp_order o inner join gr_sp_shops s on o.shopId=s.id WHERE  o.createat>$thisMonthBegin and o.createat<$thisMonthEnd and o.type=3 and o.status=1 group by o.shopId";

        $Model = new Model();
        $dtThisDaySell = $Model->query($sqlThisDaySell);
        $dtlThisDaySell_Tui = $Model->query($sqlThisDaySell_Tui);
        $dtYeaterdaySell = $Model->query($sqlYeaterdaySell);
        $dtYeaterdaySell_Tui = $Model->query($sqlYeaterdaySell_Tui);
        $dtThisMonthSell = $Model->query($sqlThisMonthSell);
        $dtThisMonthSell_Tui = $Model->query($sqlThisMonthSell_Tui);

        for ($i = 0; $i < count($dtThisDaySell); $i++) {
            for ($j = 0; $j < count($dtlThisDaySell_Tui); $j++) {
                if (floatval($dtThisDaySell[$i]["shopId"]) ==floatval($dtlThisDaySell_Tui[$j]["shopId"])) {
                    $dtThisDaySell[$i]["totalSell"] = floatval($dtThisDaySell[$i]["totalSell"]) - floatval($dtlThisDaySell_Tui[$j]["totalSell"]);
                }
            }
        }

        for ($i = 0; $i < count($dtYeaterdaySell); $i++) {
            for ($j = 0; $j < count($dtYeaterdaySell_Tui); $j++) {
                if (floatval($dtYeaterdaySell[$i]["shopId"]) ==floatval( $dtYeaterdaySell_Tui[$j]["shopId"])) {
                    $dtYeaterdaySell[$i]["totalSell"] = floatval($dtYeaterdaySell[$i]["totalSell"]) - floatval($dtYeaterdaySell_Tui[$j]["totalSell"]);
                }
            }
        }

        for ($i = 0; $i < count($dtThisMonthSell); $i++) {
            for ($j = 0; $j < count($dtThisMonthSell_Tui); $j++) {
                if (floatval($dtThisMonthSell[$i]["shopId"]) == floatval($dtThisMonthSell_Tui[$j]["shopId"])) {
                    $dtThisMonthSell[$i]["totalSell"] = floatval($dtThisMonthSell[$i]["totalSell"]) - floatval($dtThisMonthSell_Tui[$j]["totalSell"]);
                }
            }
        }

        $currentTime = time();
        //先清零今日数据
        $sql = "update gr_sp_sellsummary set currentDaySell=0,updateAt =$currentTime where timePeriod ='$thisMonth' ";
        $Model->execute($sql);

        for ($i = 0; $i < count($dtThisDaySell); $i++) {
            $shopId = $dtThisDaySell[$i]["shopId"];
            $shopName = $dtThisDaySell[$i]["name"];
            $total = $dtThisDaySell[$i]["totalSell"];
            $sqlExist = "select id from gr_sp_sellsummary where shopId=$shopId and timePeriod =$thisMonth";
            $dtExist = $Model->query($sqlExist);
            $sql = '';
            if ($dtExist[0] . count() == '') {
                $sql = "insert into gr_sp_sellsummary (shopId,shopName,timePeriod,currentMonthSell,currentDaySell,yesterdaySell,completionRate,createAt,updateAt) values ($shopId,'$shopName','$thisMonth',0,$total,0,0,$currentTime,$currentTime)";
                $res = $Model->execute($sql);
            } else {
                $sql = "update gr_sp_sellsummary set currentDaySell=$total,updateAt =$currentTime where shopId=$shopId and timePeriod ='$thisMonth' ";
                $res = $Model->execute($sql);
            }
        }

        for ($i = 0; $i < count($dtYeaterdaySell); $i++) {
            $shopId = $dtYeaterdaySell[$i]["shopId"];
            $shopName = $dtYeaterdaySell[$i]["name"];
            $total = $dtYeaterdaySell[$i]["totalSell"];
            $sqlExist = "select id from gr_sp_sellsummary where shopId=$shopId and timePeriod =$thisMonth";
            $dtExist = $Model->query($sqlExist);
            $sql = '';
            if ($dtExist[0] . count() == '') {
                $sql = "insert into gr_sp_sellsummary (shopId,shopName,timePeriod,currentMonthSell,currentDaySell,yesterdaySell,completionRate,createAt,updateAt) values ($shopId,'$shopName','$thisMonth',0,0,$total,0,$currentTime,$currentTime)";
                $res = $Model->execute($sql);
            } else {
                $sql = "update gr_sp_sellsummary set yesterdaySell= $total,updateAt =$currentTime where shopId=$shopId and timePeriod ='$thisMonth' ";
                $res = $Model->execute($sql);
            }
        }
        //获取门店指标
        $shopIndex = [];
        $sql = "select * from gr_ec.gr_dict where dict_key='$thisMonth'";
        $resIndex = $Model->query($sql);
        for ($m = 0; $m < count($resIndex); $m++) {
            $name = $resIndex[$m]["dict_group"];
            if ($name == "京华城店销售指标") {
                $shopIndexItem = [2, $resIndex[$m]["dict_value"]];
            } else if ($name == "梅岭店销售指标") {
                $shopIndexItem = [3, $resIndex[$m]["dict_value"]];
            } else if ($name == "望月路店销售指标") {
                $shopIndexItem = [4, $resIndex[$m]["dict_value"]];
            } else if ($name == "三盛店销售指标") {
                $shopIndexItem = [5, $resIndex[$m]["dict_value"]];
            } else if ($name == "时代店销售指标") {
                $shopIndexItem = [6, $resIndex[$m]["dict_value"]];
            } else if ($name == "虹桥坊店销售指标") {
                $shopIndexItem = [7, $resIndex[$m]["dict_value"]];
            } else if ($name == "彩虹汇店销售指标") {
                $shopIndexItem = [8, $resIndex[$m]["dict_value"]];
            }else if ($name == "文昌店销售指标") {
                $shopIndexItem = [9, $resIndex[$m]["dict_value"]];
            }else if ($name == "兴城路店销售指标") {
                $shopIndexItem = [10, $resIndex[$m]["dict_value"]];
            }else if ($name == "中医院店销售指标") {
                $shopIndexItem = [11, $resIndex[$m]["dict_value"]];
            }else if ($name == "星耀天地店销售指标") {
                $shopIndexItem = [12, $resIndex[$m]["dict_value"]];
            }else if ($name == "新都坊店销售指标") {
                $shopIndexItem = [13, $resIndex[$m]["dict_value"]];
            }
            $shopIndex[] = $shopIndexItem;
        }
        for ($i = 0; $i < count($dtThisMonthSell); $i++) {
            $shopId = $dtThisMonthSell[$i]["shopId"];
            $shopName = $dtThisMonthSell[$i]["name"];
            $total = $dtThisMonthSell[$i]["totalSell"];
            $thisMonthRate = 0;
            $thisMonthIndex = 0;
            for ($m = 0; $m < count($shopIndex); $m++) {
                if ("'" + $shopIndex[$m][0] + "'" == $shopId) {
                    $thisMonthRate = round(((floatval($total) / floatval($shopIndex[$m][1])) * 100), 2) . '%';
                    $thisMonthIndex = $shopIndex[$m][1];
                }
            }
            $sqlExist = "select id from gr_sp_sellsummary where shopId=$shopId and timePeriod =$thisMonth";
            $dtExist = $Model->query($sqlExist);
            $sql = '';
            if ($dtExist[0] . count() == '') {
                $sql = "insert into gr_sp_sellsummary (shopId,shopName,timePeriod,currentMonthSell,currentDaySell,yesterdaySell,completionRate,currentMonthIndex,createAt,updateAt) values ($shopId,'$shopName','$thisMonth',$total,0,0,'$thisMonthRate','$thisMonthIndex',$currentTime,$currentTime)";
                $res = $Model->execute($sql);
            } else {
                $sql = "update gr_sp_sellsummary set currentMonthSell=$total,completionRate='$thisMonthRate',currentMonthIndex='$thisMonthIndex',updateAt =$currentTime where shopId=$shopId and timePeriod ='$thisMonth' ";
                $res = $Model->execute($sql);
            }
        }
    }

}